package com.zhuang.nettydemo.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class AppClient {

    private static final String serverIp = "127.0.0.1";
    private static final int PORT_NUM = 7788;

    public static void main(String[] args) {

        NioEventLoopGroup eventLoopGroup = null;
        try {
            //1定义线程池组
            eventLoopGroup = new NioEventLoopGroup();
            //2设置助手
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup)
                //3设置通道类型
                .channel(NioSocketChannel.class)
                //4设置处理器，初始化
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new StringEncoder());
                        socketChannel.pipeline().addLast(new StringDecoder());
                        //5定义自定义处理器
                        socketChannel.pipeline().addLast(new MyClientHandler());
                    }
                });
            //6连接服务端
            ChannelFuture channelFuture = bootstrap.connect(serverIp, PORT_NUM).sync();
            //7关闭客户端连接
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }


        //设置端口号

    }
}
